<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="98px">
      <el-form-item label="竞争单位名称" prop="enterpriseId">
          <el-select
            v-model="queryParams.enterpriseId"
            filterable
            remote
            reserve-keyword
            placeholder="请选择竞争单位名称"
            :remote-method="getQueryCompeteEnterpriseList"
            :loading="loading">
            <el-option
              v-for="item in queryOptions"
              :key="item.value"
              :label="item.enterpriseName"
              :value="item.enterpriseId">
            </el-option>
          </el-select>
        </el-form-item>
<!--      <el-form-item label="施工单位" prop="constructUnit">
        <el-input
          v-model="queryParams.constructUnit"
          placeholder="请输入施工单位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="考核周期" prop="gradeName">
        <el-input
          v-model="queryParams.gradeName"
          placeholder="请输入考核周期"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地区" prop="region">
        <el-input
          v-model="queryParams.region"
          placeholder="请输入地区"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- el-form-item label="评价分数" prop="score">
        <el-input
          v-model="queryParams.score"
          placeholder="请输入评价分数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:grade:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:grade:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:grade:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:grade:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="gradeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="竞争单位名称" align="center" prop="enterpriseName" />
<!--      <el-table-column label="施工单位" align="center" prop="constructUnit" />-->
      <el-table-column label="考核周期" align="center" prop="gradeName" />
      <el-table-column label="地区" align="center" prop="region" />
      <el-table-column label="信用分" align="center" prop="score" />
      <el-table-column label="排名" align="center" prop="ranks" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:grade:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:grade:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改企业评价对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="400px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="竞争单位名称" prop="enterpriseId">
          <el-select
            v-model="form.enterpriseId"
            filterable
            remote
            reserve-keyword
            placeholder="请选择竞争单位名称"
            :remote-method="getCompeteEnterpriseList"
            :loading="loading">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.enterpriseName"
              :value="item.enterpriseId">
            </el-option>
          </el-select>
        </el-form-item>
        <!--<el-form-item label="施工单位" prop="constructUnit" style="width:330px;">
          <el-input v-model="form.constructUnit" placeholder="请输入施工单位" />
        </el-form-item>-->
        <el-form-item label="考核周期" prop="gradeName" style="width:330px;">
          <el-input v-model="form.gradeName" placeholder="请输入考核周期" />
        </el-form-item>
        <el-form-item label="地区" prop="region" style="width:330px;">
          <el-input v-model="form.region" placeholder="请输入地区" />
        </el-form-item>
        <el-form-item label="信用分" prop="score" style="width:330px;">
          <el-input v-model="form.score" placeholder="请输入信用分" />
        </el-form-item>
        <el-form-item label="排名" prop="ranks" style="width:330px;">
          <el-input v-model="form.ranks" placeholder="请输入排名" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGrade, getGrade, delGrade, addGrade, updateGrade } from "@/api/business/grade";
import { optionList} from "@/api/business/compete_enterprise";
export default {
  name: "Grade",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企业评价表格数据
      gradeList: [],
      queryOptions: [],
      options: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        enterpriseId: null,
        constructUnit: null,
        gradeName: null,
        region: null,
        score: null,
      },
      option: {
        enterpriseId: null,
        enterpriseName: null,
      },
      queryEnterpriseParams: {
        pageNum: 1,
        pageSize: 10,
        enterpriseName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        enterpriseId: [
          { required: true, message: "竞争单位不能为空", trigger: "blur" }
        ],
       /* constructUnit: [
          { required: true, message: "施工单位不能为空", trigger: "blur" }
        ],*/
        gradeName: [
          { required: true, message: "考核周期不能为空", trigger: "blur" }
        ],
        region: [
          { required: true, message: "地区不能为空", trigger: "blur" }
        ],
        score: [
          { required: true, validator: (rule, value, callback) => {
            if (!value) {
              callback(new Error('信用分不能为空'))
            } else {
              if (!this.isNumber(value)) {
                callback(new Error('请输入数字值'))
              } else {
                callback()
              }
            }
          }, trigger: ["blur","change"] }
        ],
        ranks: [
          { required: true, validator: (rule, value, callback) => {
            if (!value) {
              callback(new Error('排名不能为空'))
            } else {
              if (!this.isNumber2(value)) {
                callback(new Error('请输入整数'))
              } else {
                callback()
              }
            }
          }, trigger: ["blur","change"] }
        ],
      }
    };
  },
  created() {
    this.getList();
    optionList().then(response => {
      this.queryOptions = response.rows;
    });
  },
  methods: {
    /** 查询企业评价列表 */
    getList() {
      this.loading = true;
      listGrade(this.queryParams).then(response => {
        this.gradeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getCompeteEnterpriseList(query) {
      if (query !== '') {
        this.loading = true;
        this.queryEnterpriseParams.enterpriseName = query;
        optionList(this.queryEnterpriseParams).then(response => {
          this.loading = false;
          this.options = response.rows;
        });
      } else {
        this.options = [];
      }
    },
    getQueryCompeteEnterpriseList(query) {
      if (query !== '') {
        this.loading = true;
        this.queryEnterpriseParams.enterpriseName = query;
        optionList(this.queryEnterpriseParams).then(response => {
          this.loading = false;
          this.queryOptions = response.rows;
        });
      } else {
        this.options = [];
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        gradeId: null,
        enterpriseId: null,
        constructUnit: null,
        gradeName: null,
        region: null,
        score: null,
        ranks: null,
        creator: null,
        createTime: null,
        updateTime: null,
        remark: null,
        options: []
      };
      this.resetForm("form");
    },
    isNumber (val) {
      var regPos = /^\d+(\.\d+)?$/
      var regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/
      if (regPos.test(val) || regNeg.test(val)) {
        return true
      } else {
        return false
      }
    },
    isNumber2 (val) {
      var regPos = /^\d+(\.\d+)?$/
      var regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/
      if (regPos.test(val)) {
        return true
      } else {
        return false
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.gradeId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加企业信用分";
      optionList().then(response => {
        this.loading = false;
        this.options = response.rows;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.option.enterpriseId=row.enterpriseId;
      this.option.enterpriseName=row.enterpriseName;
      this.options.push(this.option);
      const gradeId = row.gradeId || this.ids
      getGrade(gradeId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改企业信用分";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.gradeId != null) {
            updateGrade(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGrade(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const gradeIds = row.gradeId || this.ids;
      this.$modal.confirm('是否确认删除企业评价编号为"' + gradeIds + '"的数据项？').then(function() {
        return delGrade(gradeIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/grade/export', {
        ...this.queryParams
      }, `grade_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
